#include <fstream>
#include <string>
#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include "main.h"
#include "test.h"
#include "recognize.h"



void getInfo(const string &str)
{
    string::size_type idx;
    if((idx = str.find("--version")) != string::npos) 
    {
        cout << "INFO: Version: " << __VERSION__ << endl;
    }
    else if((idx = str.find("--author")) != string::npos) 
    {
        cout << "INFO: Authors: " << __AUTHOR__ << endl;
    }
}


void runFunc(const string &str)
{
    string input = str;
    string::size_type idx;
    if((idx = input.find("test")) != string::npos) 
    {
        input.erase(0,idx + strlen("test")+1);
        if((idx = str.find("--mysql")) != string::npos)
        {
            OpMysqlTest1();
        }
    }
}




void touch(const string &str)
{
    string local = str;
    cout << "DEBUG: " << local << endl;
    ofstream outfile(local);
    if(outfile.is_open()){ 
        cout << "DEBUG: " << str << " is created. " << endl;
        outfile.close();
    }else    cout << "DEBUG: " << str << " is not created. " << endl;
}

#define DEFAULT_COLOR   "\033[0m" //  关闭所有属性
#define YELLOW  "\033[33m"      /* yellow */
#define BLUE    "\033[34m"      /* Blue */
#define WHITE   "\033[37m"      /* White */
void cat(const string &str)
{
// cat [command] file
/**
 * command:
 *          --color=yellow
*/
    fstream file;
    string tmpLine;

    // 检测command
    string args = "";
    std::size_t idx;
    if((idx = str.find('--')) != str.npos)
    {
        string tmp = str.substr(idx, str.find_first_of('='));
        if(tmp.compare("--color") == 0)
        {
            idx = str.find_first_of('=', idx); ++idx;
            std::size_t idx2 = str.find_first_of(' ', idx);
            string color = str.substr(idx, idx2 - idx);
            if(color.compare("yellow") == 0)
            {
                args = YELLOW;
                idx += 7;   // strlen("yellow");
            }
            else if(color.compare("blue") == 0)
            {
                args = BLUE;
                idx += 4;
            }
            else if(color.compare("white") == 0)
            {
                args = WHITE;
                idx += 5;
            }
            
        }
    }
// cat --color=yellow tmp.txt

    string filePath = str.substr(str.find_last_of(' ')+1); // 文件路径不支持空格的原因


    file.open(filePath, ios_base::in);
    if(file.is_open())
    {
        while(file.good())
        {
            getline(file, tmpLine);
            cout  << args << tmpLine << DEFAULT_COLOR;
        }
        file.close();
    }else{
        cout << "No such file \"" << str << "\"" << endl;
    }

}

